<?php
namespace Home\Controller;

use Utuo\ForegroundController;

class LinkController extends ForegroundController {

	public function _initialize() {
		layout(false);
		$this->setModelName('Site');
	}

	private function getCategory() {
		$this->catId = $id = I('id', 0, 'intval');
		$map['cid'] = $this->sid;
		$map['status'] = 1;
		$map['is_deleted'] = 0;
		$category = M('ArticleCategory')->where($map)->find($id);
		$pageTitle=$category['name'];
		$this->assign('category', $category);
		$this->assign("pageTitle",$pageTitle);
		$this->assign('cat_id', $id);
		return $category;
	}
	// 文章列表
	public function listAction() {
		$category = $this->getCategory();
		
		$this->doDisplay($category, 'list', $this->modelName."/Link/");
	}
	
	// 更多链接
	public function moreAction() {
		$this->getCategory();
		$list = $this->lists($this->catId, true, 12, $this->getPage());

		$totalPages = ceil($list['count'] / $this->getPageSize()); //总页数
		// 显示更多文章数据
		$this->ajaxSuccess('', $list['data']);
	}
	
	private function lists($cateId = 0, $paginate = false, $pageSize = 10, $p = 1) {
		$condition = array(
				"cid" => $this->sid,
				"is_deleted" => 0,
				"status" => 1,
				"cat_id" => $cateId,
		);
		$m = M("Link")->where($condition);
		$new_m = clone $m;
		// 分页处理
		if($paginate) {
			$m->limit($pageSize)->page($this->getPage());
			$contentCount = $new_m->count();
		}
		$contentLists = $m->order('post_time desc')->select();
		return array('count' => $contentCount, 'data' => $contentLists);
	}
	
	// 按设置的模板显示
	private function doDisplay( $category, $tplType, $rootPath ) {
		$siteThemeTpl = D("SiteThemeTpl", "Logic");
		$tpl = $siteThemeTpl->getCategoryTpl( unserialize($category['tpl_config']), $tplType );
		$this->display($tpl ? $rootPath.$tpl : $rootPath.ACTION_NAME);
	}
}